﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using KPIS.GERP.Models;
using KPIS.GERP.Models.HRM;

namespace KPIS.GERP.WCF.GHRMS
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IAbsenceReportService" in both code and config file together.
    [ServiceContract]
    public interface IAbsenceReportService
    {

        [OperationContract]
        IList<PsAbsence> GetAbsenceDaysData(string emp_seq);

        [OperationContract]
        IList<RuleAbsence> GetDaysLimitData(string emp_seq);
    }
    
    
    public class AbsenceReportService : IAbsenceReportService
    {

        public IList<PsAbsence> GetAbsenceDaysData(string emp_seq)
        {
            List<PsAbsence> result = new List<PsAbsence>();
            using (var db = new GERPContext())
            {
                var query = db.PsAbsences.Where(o =>
                    o.RecordStatus == "A");

                int empSeq;
                if (!string.IsNullOrWhiteSpace(emp_seq) &&
                    Int32.TryParse(emp_seq, out empSeq) &&
                    empSeq != 0)
                {
                    query = query.Where(o => o.EmpId == empSeq);
                }

                result = query.ToList();
            }

            return result;
        }

        public IList<RuleAbsence> GetDaysLimitData(string emp_seq)
        {
            List<RuleAbsence> result;
            using (var db = new GERPContext())
            {
                Employee employee = null;
                int empSeq;
                if (!string.IsNullOrWhiteSpace(emp_seq) &&
                    Int32.TryParse(emp_seq, out empSeq) &&
                    empSeq != 0)
                {
                    employee = db.Employees.Find(empSeq);
                }

                var query = db.RuleAbsences.Where(o =>
                    o.RecordStatus != "D");

                if (employee != null)
                {
                    query = query.Where(o => employee.EmpTypeId == o.EmpTypeId);
                }

                result = query.ToList();
            }

            return result;
        }
    }
}
